﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Subfaculty.Common.Entities;
using ClosedXML.Excel;
using Subfaculty.WebApplication.ViewModels;

namespace Subfaculty.WebApplication.Services
{
    public class ExcelExport
    {
        #region write range
        protected void WriteHead(IXLRange range, string text)
        {
            range.Style.Border.BottomBorder = XLBorderStyleValues.Thick;
            range.Style.Border.RightBorder = XLBorderStyleValues.Thick;
            range.Style.Border.LeftBorder = XLBorderStyleValues.Thick;
            range.Style.Border.TopBorder = XLBorderStyleValues.Thick;
            range.Value = text;
            range.Style.Alignment.WrapText = true;
        }

        protected void WriteVerticalHead(IXLRange range, string text)
        {
            WriteHead(range, text);
            range.Style.Alignment.TextRotation = 90;
        }

        protected void WriteTitle(IXLRange range, string text)
        {
            range.Merge().Value = text;
            range.Style.Font.Bold = true;
            range.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            range.Style.Border.OutsideBorder = XLBorderStyleValues.Thick;
        }

        protected void WriteCell(IXLRange range, object value)
        {
            range.Value = value;
            range.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            range.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
        }

        #endregion

        #region headers
        protected int MakeHeaders(IXLWorksheet worksheet, int rowIndex)
        {
            List<IXLRange> ranges = new List<IXLRange>();
            int colIndex = 0;
            foreach (var col in worksheet.Columns("A", "AE"))
            {
                var range = worksheet.Range(col.Cell(rowIndex), col.Cell(rowIndex + 4));
                ranges.Add(range);
                range.Merge();

                WriteHead(col.Cell(rowIndex + 5).AsRange(), (++colIndex).ToString());
            }

            WriteHead(ranges[0], "№");
            WriteHead(ranges[1], "Наименование дисциплины");
            WriteVerticalHead(ranges[2], "Код направл, спец");
            WriteHead(ranges[3], "Группы");
            WriteVerticalHead(ranges[4], "Всего групп в потоке");
            WriteVerticalHead(ranges[5], "Всего подгрупп");
            WriteVerticalHead(ranges[6], "Количество студентов");
            WriteVerticalHead(ranges[7], "Семестр   (1 - 12)");
            WriteVerticalHead(ranges[8], "Форма ОргСРС");
            WriteVerticalHead(ranges[9], "А Аудиторная по плану");
            WriteVerticalHead(ranges[10], "С Самост раб по плану");
            WriteVerticalHead(ranges[11], "Расчетный коэф");
            WriteVerticalHead(ranges[12], "Часов в семестре");
            WriteVerticalHead(ranges[13], "По учебн плану");
            WriteVerticalHead(ranges[14], "Всего");
            WriteVerticalHead(ranges[15], "По учебн плану");
            WriteVerticalHead(ranges[16], "Всего");
            WriteVerticalHead(ranges[17], "Текущая (Д)");
            WriteVerticalHead(ranges[18], "Предэкзамен Д, В, З)");
            WriteVerticalHead(ranges[19], "Текущая (В)");
            WriteVerticalHead(ranges[20], "Индив + текущ (З)");
            WriteVerticalHead(ranges[21], "По черчению   0,4");
            WriteVerticalHead(ranges[22], "Для остальных  0,2");
            WriteVerticalHead(ranges[23], "Рецнзир контр раб (З)");
            WriteVerticalHead(ranges[24], "Проверка контр (В)");
            WriteVerticalHead(ranges[25], "Экзамены 0,3");
            WriteVerticalHead(ranges[26], "Курсовой проект (В,З)");
            WriteVerticalHead(ranges[27], "Курсовая работа (В,З)");
            WriteVerticalHead(ranges[28], "Организуемая СРС (Д)");
            WriteVerticalHead(ranges[29], "Рейтинг (Д,В)");
            WriteVerticalHead(ranges[30], "ВСЕГО");

            return rowIndex + 6;
        }

        //rotected virtual int MakeNotation(StudyLoadViewModel studyLoad, IXLWorksheet worksheet, int rowIndex);

        #endregion

        #region study load export
        protected int ExportSemester(IXLWorksheet worksheet, int rowIndex, IEnumerable<StudyLoadItemViewModel> items, int semester)
        {
            int yearSemester = semester % 2 == 0 ? 2 : 1;
            WriteTitle(worksheet.Range(rowIndex, 1, rowIndex, 31), yearSemester.ToString("0 семестер"));
            rowIndex++;
            for (int i = 0; i < items.Count(); ++i)
            {
                ExportOneRow(worksheet, i + 1, rowIndex++, items.ElementAt(i));
            }

            return rowIndex;
        }

        protected void ExportOneRow(IXLWorksheet worksheet, int index, int rowIndex, StudyLoadItemViewModel slViewItem)
        {
            var rowRange = worksheet.Row(rowIndex).AsRange();
            //WriteCell(rowRange, );
            WriteCell(rowRange.Cell(1, 1).AsRange(), index);
            WriteCell(rowRange.Cell(1, 2).AsRange(), slViewItem.Discipline);
            WriteCell(rowRange.Cell(1, 3).AsRange(), "");
            WriteCell(rowRange.Cell(1, 4).AsRange(), slViewItem.GroupList);
            WriteCell(rowRange.Cell(1, 5).AsRange(), slViewItem.CountGroup);
            WriteCell(rowRange.Cell(1, 6).AsRange(), slViewItem.CountSubGroup);
            WriteCell(rowRange.Cell(1, 7).AsRange(), slViewItem.CountStudent);

            WriteCell(rowRange.Cell(1, 8).AsRange(), slViewItem.Semester);
            WriteCell(rowRange.Cell(1, 9).AsRange(), slViewItem.TypeSRS);
            WriteCell(rowRange.Cell(1, 10).AsRange(), slViewItem.AuditorHoursPlan);
            WriteCell(rowRange.Cell(1, 11).AsRange(), slViewItem.SRSHoursPlan);
            WriteCell(rowRange.Cell(1, 12).AsRange(), slViewItem.KoofSRS);
            WriteCell(rowRange.Cell(1, 13).AsRange(), slViewItem.HoursLecture);
            WriteCell(rowRange.Cell(1, 14).AsRange(), slViewItem.HoursPracticePlan);
            WriteCell(rowRange.Cell(1, 15).AsRange(), slViewItem.HoursPracticeAll);
            WriteCell(rowRange.Cell(1, 16).AsRange(), slViewItem.HoursLabPlan);
            WriteCell(rowRange.Cell(1, 17).AsRange(), slViewItem.HoursLabAll);
            WriteCell(rowRange.Cell(1, 18).AsRange(), slViewItem.ConsultationFullTimeHours);
            WriteCell(rowRange.Cell(1, 19).AsRange(), slViewItem.ConsultationExamHours);
            WriteCell(rowRange.Cell(1, 20).AsRange(), slViewItem.ConsultationEveningHours);
            WriteCell(rowRange.Cell(1, 21).AsRange(), slViewItem.ConsultationDistanseHours);
            WriteCell(rowRange.Cell(1, 22).AsRange(), "");
            WriteCell(rowRange.Cell(1, 23).AsRange(), "");
            WriteCell(rowRange.Cell(1, 24).AsRange(), slViewItem.ControlWorkDistanseHours);
            WriteCell(rowRange.Cell(1, 25).AsRange(), slViewItem.ControlWorkEveningHours);
            WriteCell(rowRange.Cell(1, 26).AsRange(), slViewItem.ExamHours);
            WriteCell(rowRange.Cell(1, 27).AsRange(), slViewItem.CourseProjectEveningDistanseHours);
            WriteCell(rowRange.Cell(1, 28).AsRange(), slViewItem.CourseWorkEveningDistanseHours);
            WriteCell(rowRange.Cell(1, 29).AsRange(), slViewItem.SRSHours);
            WriteCell(rowRange.Cell(1, 30).AsRange(), slViewItem.RatingHours);
            WriteCell(rowRange.Cell(1, 31).AsRange(), slViewItem.AllHours);
        }
        #endregion
    }
}
